package com.lhl.jwt.service;

import com.lhl.jwt.domain.entity.SysUser;
import com.lhl.jwt.excepiton.JwtException;
import com.lhl.jwt.repository.UserRepository;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @className: com.lhl.jwt.service.AuthService
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-12-21 15:13
 **/
@Api(tags = "AuthService", produces = "AuthService")
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthService {
    private final UserRepository userRepository;

    public SysUser findByOpenId(String openId){
        return userRepository.findByOpenId(openId).orElse(null);
    }

    public SysUser findByUsername(String username){
        return userRepository.findByUsername(username).orElseThrow(()->new JwtException("没找到对应的用户"));
    }

    public SysUser register(SysUser sysUser){
       return userRepository.save(sysUser);
    }
}
